home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / standards / sgml / nist / parse2a / parse2a.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-13  |  8.4 KB  |  295 lines

  1. /* National Institute of Standards and Technology (NIST)
  2. /* National Computer System Laboratory (NCSL)
  3. /* Office Systems Engineering (OSE) Group
  4. /* ********************************************************************
  5. /*                            D I S C L A I M E R
  6. /*                              (March 8, 1989)
  7. /*  
  8. /* There is no warranty for the NIST NCSL OSE SGML parser and/or the NIST
  9. /* NCSL OSE SGML parser validation suite.  If the SGML parser and/or
  10. /* validation suite is modified by someone else and passed on, NIST wants
  11. /* the parser's recipients to know that what they have is not what NIST
  12. /* distributed, so that any problems introduced by others will not
  13. /* reflect on our reputation.
  14. /* 
  15. /* Policies
  16. /* 
  17. /* 1. Anyone may copy and distribute verbatim copies of the SGML source
  18. /* code as received in any medium.
  19. /* 
  20. /* 2. Anyone may modify your copy or copies of SGML parser source code or
  21. /* any portion of it, and copy and distribute such modifications provided
  22. /* that all modifications are clearly associated with the entity that
  23. /* performs the modifications.
  24. /* 
  25. /* NO WARRANTY
  26. /* ===========
  27. /* 
  28. /* NIST PROVIDES ABSOLUTELY NO WARRANTY.  THE SGML PARSER AND VALIDATION
  29. /* SUITE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  30. /* EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  31. /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  32. /* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS
  33. /* WITH YOU.  SHOULD THE SGML PARSER OR VALIDATION SUITE PROVE DEFECTIVE,
  34. /* YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  35. /* 
  36. /* IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL NIST BE LIABLE FOR
  37. /* DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL,
  38. /* INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  39. /* INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA
  40. /* BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A
  41. /* FAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY
  42. /* NIST) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF
  43. /* SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  44. */
  45.  
  46. #include <stdio.h>
  47. #include <fcntl.h>
  48. #include <sys/types.h>
  49. #include <sys/stat.h>
  50.  
  51. #define    COMMA      -21
  52. #define    AND        -22
  53. #define    OR         -23
  54.  
  55. #define NAMELEN 8
  56. #define NUMELTS 256
  57. #define NUMCOLS NUMELTS
  58. #define NUMROWS NUMELTS
  59. int errflag = 0;
  60. void saferead(), doread(), showmatrix(), heading(), setcell(), error(),
  61. analyze();
  62. /* ================================================================ */
  63.  
  64. typedef struct {
  65.    char Rcol[NUMCOLS];
  66. }ROW;
  67.  
  68. typedef struct {
  69.    int  Smin;
  70.    char Sname[8];
  71.    int  Snbr;
  72. }SYMBOL;
  73.  
  74. typedef struct {
  75.    int Dtoken;
  76.    int Doi;
  77.    int Dcontreq;
  78. } DTDREC;
  79. /* ================================================================ */
  80. SYMBOL symbol[NUMROWS];
  81. ROW Rrow[NUMROWS];
  82. unsigned char debug;
  83. /* ================================================================ */
  84. main(argc, argv)
  85. int argc;
  86. char *argv[];
  87. {
  88.  
  89.    int infile,numtokens,roottoken;
  90.    register int j;
  91.    char fname[128];
  92.  
  93.    infile = doinit(argc, argv, fname);
  94.    saferead(infile, &numtokens, sizeof(numtokens));
  95.    if (numtokens >= NUMROWS)
  96.       error("too many tokens for closure tables");
  97.    saferead(infile, &roottoken, sizeof(roottoken));
  98.    /* read in all the symbols */
  99.    for (j = 0; j < numtokens; j++){
  100.       saferead(infile, &symbol[j], sizeof(SYMBOL));
  101.    }
  102.    for (j = 0; j < numtokens; j++)
  103.       doread(infile, j, symbol);
  104.    close(infile);
  105.    showmatrix(numtokens);
  106.    for (j = 0; ; j++){
  107.       if (doclosure(numtokens) == 0)
  108.          break;
  109.    }
  110.    showmatrix(numtokens);
  111.    analyze(roottoken, numtokens, symbol);
  112.    exit(0);
  113. }
  114. /* ================================================================ */
  115. void doread(infile, tokennbr, symbol)
  116. int infile, tokennbr;
  117. SYMBOL symbol[];
  118. {
  119.    DTDREC tokeninfo;
  120.    int andcount;
  121.  
  122.    saferead(infile, &tokeninfo, sizeof(tokeninfo)) ;
  123.    switch(tokeninfo.Dtoken) {
  124.    case COMMA:
  125.    case OR:
  126.       doread(infile, tokennbr, symbol);
  127.       doread(infile, tokennbr, symbol);
  128.       break;
  129.    case AND:
  130.       saferead(infile, &andcount, sizeof(andcount));
  131.       while(andcount--)
  132.          doread(infile, tokennbr, symbol);
  133.       break;
  134.    default:
  135.       setcell(tokennbr, tokeninfo.Dtoken, symbol);
  136.    }
  137. }
  138. /* ================================================================ */
  139. void setcell(row, col, symbol)
  140. int row;
  141. register int col;
  142. SYMBOL symbol[];
  143. {
  144.    register char *temp = Rrow[row].Rcol;
  145. #ifdef JJJ
  146.    if (col == 0) {
  147.       strncpy(temp, symbol[row].Sname, NAMELEN);
  148.       *(temp + NAMELEN) = 0;
  149.       fprintf(stdout,"element '%s' contains root element as subelement\n",
  150.           temp);
  151.       return;
  152.    }
  153. #endif
  154.    if (col < 0)
  155.       col = 0;
  156.    *(temp + col) = 1;
  157. }
  158.  
  159. /* ================================================================ */
  160. void showmatrix(count)
  161. register int count;
  162. {
  163.    register int j, k;
  164.    return;
  165.    for (j = 0; j < count; j++){
  166.       for (k = 0; k < count; k++)
  167.          printf("[%02x]", Rrow[j].Rcol[k]);
  168.       printf("\n");
  169.    }
  170.    printf("\n\n");
  171. }
  172.  
  173. /* ================================================================ */
  174. int doclosure(count)
  175. int count;
  176. {
  177.    int row;
  178.    int chgdsomething = 0;
  179.    register col, tempcol;
  180.    for(row = 0; row < count; row++) {
  181.       for(col = 1; col < count ;col++) {
  182.          if (debug == 'Y')
  183.             printf("<row = %d, col = %d>\n", row, col);
  184.          if(Rrow[row].Rcol[col] == 1) {
  185.             for(tempcol = 0; tempcol < count; tempcol++)
  186.             {
  187.                if (debug == 'Y')
  188.                   printf("Rrow[%d].Rcol[%d] = [%d]\n",
  189.                       col, tempcol, Rrow[col].Rcol[tempcol]);
  190.                if(Rrow[col].Rcol[tempcol] == 1) {
  191.                   if (Rrow[row].Rcol[tempcol] != 1)
  192.                      chgdsomething++;
  193.                   Rrow[row].Rcol[tempcol] = 1;
  194.                   if (debug == 'Y')
  195.                      showmatrix(count);
  196.                }
  197.             }
  198.          }
  199.       }
  200.    }
  201.    return(chgdsomething);
  202. }
  203. /* ================================================================ */
  204. void saferead(file, buf, len)
  205. int file;
  206. unsigned char *buf;
  207. unsigned len;
  208. {
  209.    int j;
  210.    static int readcount = 0;
  211.    readcount++;
  212.    if ((j = read(file, buf, len)) != len) {
  213.       error("read failure\n");
  214.       j = j;
  215.    }
  216. }
  217. /* ================================================================ */
  218. void error(msg)
  219. unsigned char *msg;
  220. {
  221.    printf(msg);
  222.    exit(1);
  223. }
  224. /* ================================================================ */
  225. doinit(argc, argv, fname)
  226. int argc;
  227. unsigned char *argv[];
  228. unsigned char *fname;
  229. {
  230.    int infile;
  231.    char temp[128];
  232.  
  233.    strcpy(fname, "dtdfile.sgm");
  234.    if ((infile = open(fname, O_RDONLY)) == -1){
  235.       sprintf(temp,"unable to open %s", fname);
  236.       error(temp);
  237.    }
  238.    return(infile);
  239. }
  240. /* ================================================================ */
  241. void analyze(root, count, symbol)
  242. int root;
  243. register int count;
  244. SYMBOL symbol[];
  245. {
  246.    register int j;
  247.    char temp[128];
  248.    ;
  249.    for(j = 0; j < count; j++)
  250.       if (Rrow[root].Rcol[j] != 1){
  251.          if (j == root)
  252.             continue;
  253.          strncpy(temp, symbol[j].Sname, NAMELEN);
  254.          *(temp + NAMELEN) = '\0';
  255.          fprintf(stdout, "element %s may not have path to root element\n", temp);
  256. #ifdef JJJ
  257.          printf("Press ENTER to continue\n");
  258.          getchar();
  259. #endif
  260.          errflag = 1;
  261.       }
  262.    for (j = 0; j < count; j++)
  263.       if (Rrow[j].Rcol[0] != 1) {
  264.          strncpy(temp, symbol[j].Sname, NAMELEN);
  265.          *(temp + NAMELEN) = '\0';
  266.          fprintf(stdout,"element %s may not have path to a terminal element\n",
  267.              temp);
  268. #ifdef JJJ
  269.          printf("Press ENTER to continue\n");
  270.          getchar();
  271.  
  272. #endif
  273.          errflag = 1;
  274.       }
  275. }
  276. /* ================================================================ */
  277.  
  278. /*----------------------------------------------*/
  279. /*         HEADING         */
  280. /* Simply prints a informatory heading */
  281. /* when the document parser is invoked */
  282. /* describing the document name to be  */
  283. /* parsed.              */
  284. /*----------------------------------------------*/
  285.  
  286. void heading()
  287. {
  288.    printf("\n                               CLOSURE\n");
  289.    printf("                      Determine Closure of Elements\n");
  290.    printf("          --------------------------------------------------\n");
  291.    printf("                      10FEB87      Version 1.02\n");
  292.    printf("          --------------------------------------------------\n\n");
  293.    return;
  294. }
  295.